#include<bits/stdc++.h>
#define sd(n) scanf("%d",&n) 
#define sld(n) scanf("%lld",&n)
#define pd(n) printf("%d", (n))
#define pld(n) printf("%lld", n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fi first
#define se second
const int N=2e5;
#define INF 0x7fffffff
typedef long long int ll;
using namespace std;
//----------------------------------------------------------------------------//
int dp[1005];
void solve()
{
	int n,m;
	sd(n);sd(m);
	//cout<<(m-n/3)-2<<'\n';

	vector<int>v1;
	int num1=m,num0=n-m;
	

	for(int i=n;i>=1;i--)
	{
		int j=n-i+1;

		if(i<=num1) v1.push_back(1),num1--;
		else
		{
			if(j%3==1)
			{
				v1.push_back(1);
				num1--;
			}
			else
			{
				v1.push_back(0);
				num0--;
			}
		}
	}

	int sz=v1.size();
	for(int i=0;i<sz;i++)
	{
		if(v1[i]==0) dp[i+1]++;
	}
	for(int i=1;i<=n;i++) dp[i]+=dp[i-1];

	int res=0;
	for(int i=3;i<=n;i++)
	{
		int cur=dp[i]-dp[i-3];
		if(cur<2)
		{
			res++;
		}
	}

	pd(res);
	puts("");
	
}

int main()
{
	int T=1;
	//sd(T);
	while (T--)
	{
		solve();
	}
	return 0;
}